package com.test.realcityalfa;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app._ActionBarSherlockTrojanHorse;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.test.realcityalfa.actionbar.ActionBarBuilder;
import com.test.realcityalfa.actionbar.ActionBarSettings;
import com.test.realcityalfa.activityswitchers.ActivitySwitch;
import com.test.realcityalfa.connection.InternetConnection;
import com.test.realcityalfa.connection.RequestMethodFactory;
import com.test.realcityalfa.exception.NoInternetConnectionException;
import com.test.realcityalfa.frmwrk.BaseActivity;
import com.test.realcityalfa.frmwrk.CustomAsyncTask;
import com.test.realcityalfa.frmwrk.MyApplication;


public class SplashScreenActivity extends BaseActivity
{	
	//test
	
	
	
	private InternetConnection internetConnection;		
	private InternetAccessVerification internetVerifier;
	ActivitySwitch activitySwitch;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		//setContentView(com.test.realcityalfa.R.layout.main_activity);
		setContentView(com.test.realcityalfa.R.layout.splashscreen_activity);
		
		doTestActions();
		
		ActionBarBuilder barBulider = new ActionBarBuilder(this);
		ActionBarSettings barSettings = new ActionBarSettings();
		
		barSettings.setVisibility(false);
		
		ActionBar bar = barBulider.createActionBar();
		
		barBulider.applyActionBarSettings(bar, barSettings);
		
				
	}
	
	public void doCancelSwitch(View v)
	{
		activitySwitch.cancelSwitching();
	}
	private void doTestActions()
	{
		internetVerifier = new InternetAccessVerification(getApplicationContext());
		internetVerifier.execute();		
	}
	
	@Override
	protected void onPause() 
	{
		/*
		if(this.isFinishing())
		{
			internetVerifier.cancel(true);		
			this.activitySwitch.cancelSwitching();
		}
		*/
		super.onPause();
	}

	

	@Override
	protected void onResume() 
	{
		if(this.internetVerifier.getStatus() == AsyncTask.Status.FINISHED)
		{
			Log.d(getClass().getName(), "AsyncTask was already finished");
			new ActivitySwitch(CommercialActivity.class).goToNextActivity(this, 0);
		}
		super.onResume();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		
	
		internetVerifier.cancel(true);		
		this.activitySwitch.cancelSwitching();
		Log.d("splashScreenActivity", "Stoping threads...");
		super.onStop();
	}

	@Override
	protected void onDestroy() 
	{
		//MyApplication app = (MyApplication) getApplication();
		//app.setUpBeforeExit();
		/*
		while(internetVerifier.isCancelled()){}
		super.onDestroy();
		*/		
		internetVerifier.cancel(true);
		this.activitySwitch.cancelSwitching();
		super.onDestroy();
	}

	
	
	
	class InternetAccessVerification extends CustomAsyncTask
	{
		protected int delayInMillis;
		public InternetAccessVerification(Context context) 
		{
			super(context);
		}

		@Override
		protected Object asyncOperation() throws Throwable 
		{
			internetConnection = new InternetConnection(RequestMethodFactory.GET_METHOD);
			
			try
			{
				internetConnection.isConnectionAvailable(SplashScreenActivity.this);
				
				Log.d("SplashScreenActivity", "Connection exists");
				
				activitySwitch = new ActivitySwitch(CommercialActivity.class);
				onProgressUpdate(activitySwitch);
				
				return null;
			}
			catch(NoInternetConnectionException ex)
			{
				Log.d("SplashScreenActivity", "No connection");
				
				activitySwitch = new ActivitySwitch(DashboardActivity.class);
				onProgressUpdate(activitySwitch);
			}			
			return null; 			
		}

		@Override
		protected void onResult(Object result) throws Exception 
		{	
			Toast.makeText(getApplicationContext(), "Finished", Toast.LENGTH_SHORT).show();
		}

		@Override
		protected void onProgress(Object update) throws Exception 
		{	
			final Object[] object = (Object[]) update;
			((ActivitySwitch) object[0]).goToNextActivity(SplashScreenActivity.this, 2000);
			
		}
	}

	
}
